package spring.bean;

import java.util.List;

import spring.util.Encryption;
import hibernate.dao.SexDAO;
import hibernate.dao.UserDAO;

import hibernate.model.object.City;
import hibernate.model.object.Sex;
import hibernate.model.object.User;

public class UserServiceImpl implements UserService {
    private UserDAO userDAO;
    private SexDAO sexDAO;
    public UserServiceImpl() {
        super();
      System.out.println("UserServiceImpl create!");
    }
    
    public List<Sex> getSexData(){
    	return sexDAO.findBothSex();
    }
    
    public String reg(User user) throws Exception{
    	
    	Sex sex = userDAO.getEntity(Sex.class, user.getSexId());
    	City city = userDAO.getEntity(City.class, user.getCityId());
    	
    	user.setSex(sex);
    	user.setCity(city);
    	user.setStatus("new");
    	user.setPassword(Encryption.getEncryptStr(user.getPassword()));
    	
    	userDAO.addEntity(user);
        return user.getUsername();
    }
    
    public User verifyUser(String email, String password) throws Exception{
    	List<User> users = userDAO.getUserByEmailAndPwd(email, password);
    	
    	if(users.size()==0){
    		return null;
    	}
    	return users.get(0);
    }
    
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public UserDAO getUserDAO() {
        return userDAO;
    }
	public SexDAO getSexDAO() {
		return sexDAO;
	}
	public void setSexDAO(SexDAO sexDAO) {
		this.sexDAO = sexDAO;
	}
    
}
